/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lct.eq2.backup;

import com.lct.eq2.data.Attack;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 *
 * @author Me
 */
public class TestClient {
       public static void main (String args[]) {
           // Create a direct buffer to get bytes from socket.
           // Direct buffers should be long-lived and be reused as much as possible.
           ByteBuffer buf = ByteBuffer.allocateDirect(1024);
           SocketChannel sChannel = null;
           
           // Create a non-blocking socket and check for connections
           try {
               sChannel = createSocketChannel("localhost", 13267); //13267

               // Before the socket is usable, the connection must be completed
               // by calling finishConnect(), which is non-blocking
               while (!sChannel.finishConnect()) {
               // Do something else
               }
               
               sChannel.write(ByteBuffer.wrap("Testing!".getBytes()));
               sChannel.close();
//               Attack attack = new Attack();
//               attack.setAttackName("Testing Name");
//               attack.setAttacker("Me!");
//               attack.setAmountOfAttackDamage(1205);
//           
//               ByteArrayOutputStream bStream = new ByteArrayOutputStream();
//               ObjectOutputStream oStream = new ObjectOutputStream(bStream);
//               
//               oStream.writeObject(attack);
//               byte[] byteVal = bStream.toByteArray();
//               ByteBuffer buff = ByteBuffer.allocate(byteVal.length);
//               buff.wrap(byteVal);
//               
//               sChannel.write(buff);
               
           } catch (IOException e) {
               //nothing
           }
           
//           try {
//               // Clear the buffer and read bytes from socket
//               buf.clear();
//               int numBytesRead = sChannel.read(buf);
//
//               if (numBytesRead == -1) {
//                   // No more bytes can be read from the channel
//                   sChannel.close();
//               } else {
//                   // To read the bytes, flip the buffer
//                   buf.flip();
//               // Read the bytes from the buffer ...;
//               
//               }
//           } catch (IOException e) {
//           // Connection may have been closed
//           }

       }
    
       // Creates a non-blocking socket channel for the specified host name and port.
    // connect() is called on the new channel before it is returned.
    public static SocketChannel createSocketChannel(String hostName, int port) throws IOException {
        // Create a non-blocking socket channel
        SocketChannel sChannel = SocketChannel.open();
        sChannel.configureBlocking(false);

        // Send a connection request to the server; this method is non-blocking
        sChannel.connect(new InetSocketAddress(hostName, port));
        return sChannel;
    }
}
